//
//  ATLoginSmsCell.m
//  Postre
//
//  Created by CoderLT on 2017/6/1.
//  Copyright © 2017年 CoderLT. All rights reserved.
//

#import "ATLoginSmsCell.h"

@interface ATLoginSmsCell() {
    int timeCount;
}

@property (nonatomic, strong) NSTimer *timer;

@end
@implementation ATLoginSmsCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (IBAction)didClickButton:(id)sender {
    if (self.didClickButton) {
        self.didClickButton(self);
    }
}

- (void)setGetSMSCodeDelay {
    [self setGetSMSCodeDelay:60];
}
- (void)setGetSMSCodeDelay:(NSUInteger)delay {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1
                                                  target:self
                                                selector:@selector(updateTime)
                                                userInfo:nil
                                                 repeats:YES];
    timeCount = (int)delay;
    [self updateTime];
}

- (void)updateTime
{
    if (--timeCount > 0) {
        self.button.enabled = NO;
        [self.button setTitle:[NSString stringWithFormat:@"%ds", timeCount] forState:UIControlStateNormal];
    }
    else {
        [self.timer invalidate];
        [self.button setTitle:@"获取验证码" forState:UIControlStateNormal];
        self.button.enabled = YES;
    }
}

- (void)removeFromSuperview {
    [super removeFromSuperview];
    [_timer invalidate];
    _timer = nil;
}
- (void)dealloc {
    [self.timer invalidate];
    self.timer = nil;
}

@end
